package com.yonyou.pmclouds.projectpart.processor;

import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.projectpart.entity.ProjectPartVO;
import com.yonyou.pmclouds.projectpart.mapper.ProjectpartMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 工程部位根节点检查
 */
@Component
public class ProjectpartRootCheckProcessor implements SingleProcessor<ProjectPartVO>{

    @Autowired
    private ProjectpartMapper projectpartMapper;

    @Override
    public void process(ProjectPartVO vo, ProjectPartVO orig) throws BusinessException {
        if(StringUtils.isEmpty(vo.getPkParent()) || "topVirtual".equals(vo.getPkParent())){
            ProjectPartVO qryVO = new ProjectPartVO();
            qryVO.setPkTenant(RuntimeEnvironment.getTenantId());
            qryVO.setPkProject(vo.getPkProject());
            qryVO.setInnerCodeLen(3);
            List<ProjectPartVO> result = projectpartMapper.queryVOsByCondition(qryVO);
            if(result.size() > 0){
                throw new BusinessException("虚拟根节点(工程部位)下只允许创建一个实际根节点");
            }
            vo.setPkParent("");
        }

    }
}
